--
-- _*_ coding:utf-8 _*_
-- **********************************************************
-- * Author        : 李阳
-- * Email         : monogy@163.com
-- * Create time   : 2025-10-28 00:08
-- * Last modified : 2025-10-28 00:08
-- * Filename      : autoHeader.lua
-- * Description   : 新文件自动添加文件头，可使用<F2>进行手动添加/更新文件头
-- *********************************************************/

-- 查找命令:h lua-vim-options
-- 当前文件类型：local filety = vim.api.nvim_buf_get_option(0, "filetype")，，，但不对
-- 当前文件类型：local filet = vim.bo.filetype，，，但不对

local api = vim.api
local filename = vim.fn.expand("%:t")
local startpos = string.find(filename, ".", 1, true)
local filetype

if startpos ~= nil then
  filetype = string.sub(filename, startpos + 1)
end

-- 检测是否已经添加了完整的文件头
-- 添加过后只进行更新
-- 未添加将插入完整的文件头
function HeaderAdd()
  local n = 1
  local linenum = api.nvim_buf_line_count(0)
  while n <= 10 and linenum > 5 do
    local line = vim.fn.getline(n)
    local pos1 = string.find(line, "Last modified", 1, true)
    if pos1 ~= nil then
      HeaerUpdateCmd()
      print("ℹ️(" .. vim.fn.strftime("%Y-%m-%d %H:%M") .. ") ===> Successful in updating the copyright.")
      return
    end
    n = n + 1
  end
  HeaderAddCmd()
  print("ℹ️(" .. vim.fn.strftime("%Y-%m-%d %H:%M") .. ") => Successful in adding the copyright.")
  -- 行数小于2，说明为新文件，插入后可以到第13行
  if linenum < 2 then
    vim.cmd(":13")
  end
end

-- 插入文件头格式
function HeaderAddCmd()
  local lines, env
  if filetype == "lua" then
    lines = {
      "--",
      "-- _*_ coding:utf-8 _*_",
      "-- **********************************************************",
      "-- * Author        : 李阳",
      "-- * Email         : monogy@163.com",
      "-- * Create time   : " .. vim.fn.strftime("%Y-%m-%d %H:%M"),
      "-- * Last modified : " .. vim.fn.strftime("%Y-%m-%d %H:%M"),
      "-- * Filename      : " .. vim.fn.expand("%:t"),
      "-- * Description   : ",
      "-- *********************************************************/",
      "",
      "return{",
      "",
      "}",
    }
  else
    if filetype == "sh" then
      env = "#!/bin/bash"
    elseif filetype == "py" then
      env = "#!/usr/bin/env python"
    -- elseif filetype == "c" or filetype == "cpp" or filetype == "h" then
    else
      env = "/*"
    end

    lines = {
      env,
      "# _*_ coding:utf-8 _*_",
      "# **********************************************************",
      "# * Author        : 李阳",
      "# * Email         : monogy@163.com",
      "# * Create time   : " .. vim.fn.strftime("%Y-%m-%d %H:%M"),
      "# * Last modified : " .. vim.fn.strftime("%Y-%m-%d %H:%M"),
      "# * Filename      : " .. vim.fn.expand("%:t"),
      "# * Description   : ",
      "# *********************************************************/",
      "",
      "",
    }
  end
  -- 插入行
  api.nvim_buf_set_lines(0, 0, 0, false, lines)
end

-- 更新文件头中最后修改时间和文件名称
function HeaerUpdateCmd()
  local lines
  if filetype == "lua" then
    lines = {
      "-- * Last modified : " .. vim.fn.strftime("%Y-%m-%d %H:%M"),
      "-- * Filename      : " .. vim.fn.expand("%:t"),
    }
  -- elseif filetype == "sh" or filetype == "py" or filetype == "h" or filetype == "cpp" or filetype == "c" then
  else
    lines = {
      "# * Last modified : " .. vim.fn.strftime("%Y-%m-%d %H:%M"),
      "# * Filename      : " .. vim.fn.expand("%:t"),
    }
  end
  -- 替换行
  api.nvim_buf_set_lines(0, 6, 8, false, lines)
end

-- 新文件自动添加文件头
api.nvim_create_autocmd("BufNewFile", {
  pattern = { "*.py", "*.sh", "*.cpp", "*.c", "*.lua", "*.h" }, -- 匹配文件类型
  callback = function()
    HeaderAdd()
  end,
})

vim.api.nvim_create_user_command(
  "AddHeader", -- 命令名称
  function(opts)
    HeaderAdd()
  end,
  { nargs = "?", desc = "增加文件头" } -- 可选参数
)
-- vim.api.nvim_set_keymap("n", "<F2>", ":AddHeader<CR>", { noremap = true, silent = true })
vim.keymap.set("n", "<F2>", function()
  HeaderAdd()
end, { desc = "增加文件头" })
